<template>
	<div class="subscribe">
		<myheader :type_title="type_title" :title="title"></myheader>
		<div class="info">
			<div class="info-W">
				<span><img src="../../../static/image/no_data.png" /></span>
				<p>您还没有设置任何订阅</p>
				<div>设置订阅条件有符合您需求的房源时，我们会第一时间通知您，一手好房抢先获得！</div>
				<router-link to="/subscribe_term">创建订阅</router-link>
			</div>
			<div class="info-Y" v-for="(item,index) in subData">
				<div class="Y-s">
					<span id="add">{{item.collect_add}}</span> | 
					<span id="money">{{item.collect_money}}</span> | 
					<span id="size">{{item.collect_size}}</span> | 
					<span id="bus">{{item.collect_bus}}</span>				
				</div>				
				<div class="Y-x">
					<div hidden="hidden" class="left">共<span id="num">0</span>条订阅结果</div>
					<div class="right">
						<button class="update" @click="update(index,$event)"></button>
						<p hidden="hidden" id="subId">{{item.collect_id}}</p>
						<button class="delete" @click="deleteSub(index,$event)"></button>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import myheader from '@/components/user/myheader'
	export default {
		name: "subscribe",
		data() {
			return {
				title:"我的订阅",
				type_title:"",
				subData:"",
			}
		},
		components:{
			myheader
		},
		mounted(){
//			window.location.reload()
			var userId = localStorage.getItem("userId");
			//查询用户订阅条件的总数
			var _this = this;
			_this.axios.get(_this.ajaxUrls+'/api/User/collectcount?id='+userId).then(function(res){
				if(res.data == 0){
					$(".info-W").show();
					$(".info-Y").hide();
				}else{
					//查询我的订阅条件的信息
					_this.axios.get(_this.ajaxUrls+'/api/User/collectselect?id='+userId).then(function(res){
						$(".info-Y").show();
						$(".info-W").hide();
						_this.subData = res.data;						
					}).catch(function(err){
						console.log(err)
					})
				}
			}).catch(function(err){
				console.log(err)				
			})
		},
		methods:{
			deleteSub(index,e){
				//得到当前用户收藏的id删除
				var userId = localStorage.getItem("userId");
				var subId = e.currentTarget.previousElementSibling.innerHTML;
				var _this = this;
				_this.axios.get(_this.ajaxUrls+'/api/User/deleteSubId?userId='+userId+'&subId='+subId).then(function(res){
					window.location.reload();					
				}).catch(function(err){
					console.log(err)
				})
			},
			update(index,e){
				//根据subId删除订阅条件，调到添加订阅条件
				var userId = localStorage.getItem("userId");
				var subId =  e.currentTarget.nextElementSibling.innerHTML;
				var _this = this;
				_this.axios.get(_this.ajaxUrls+'/api/User/deleteSubId?userId='+userId+'&subId='+subId).then(function(res){
					_this.$router.push('/subscribe_term');				
				}).catch(function(err){
					console.log(err)
				})
			}
		}
	};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.subscribe{
		width: 100%;
		height: auto;
		background: #F1F1F1;
	}
	.nav{
		width: 100%;
		height: 0.8rem;
		margin-top: 0.05rem;
		background: white;	
		display: flex;
		justify-content: flex-start;
		align-items: center;	
	}
	.nav span{
		width: 50%;
		height: 100%;
		display: inline-block;
		text-align: center;
		line-height: 0.8rem;
		color: #666666;
		font-size: 0.3rem;
	}
	.nav .active{
		border-bottom: 4px solid #F3B240;
		box-sizing: border-box;
	}
	.info{
		width: 100%;
		height: auto;
		margin-top: 0.05rem;
		background: white;
	}
	.info-W{
		width: 100%;
		height: 5.5rem;
		padding: 0 0.3rem;
		box-sizing: border-box;
	}
	.info-W span{
		display: inline-block;
		width: 100%;
		height: 2.8rem;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.info-W img{
		width: 1.35rem;
		height: 1.15rem;
	}
	.info-W p{
		width: 100%;
		text-align: center;
		color: #999999;
		font-size: 0.35rem;
	}
	.info-W div{
		width: 100%;
		height: 0.6rem;
		color: #999999;
		font-size: 0.3rem;
		text-align: center;
		margin-top: 0.5rem;
	}
	.info-W a{
		display: inline-block;
		width: 100%;
		height: 0.7rem;
		text-align: center;
		line-height: 0.7rem;
		color: white;
		font-size: 0.35rem;
		border-radius: 0.1rem;
		background: #a5b9d1;
		margin-top: 0.5rem;
	}
	.info-Y{
		width: 100%;
		height: 3rem;
		display: flex;
		flex-wrap: wrap;
		padding: 0.5rem;
		box-sizing: border-box;
	}
	.Y-s{
		width: 100%;
		height: 1rem;
		color: #666666;
		font-size: 0.3rem;
	}
	.Y-s span{
		color: #999999;
	}
	.Y-x{
		width: 100%;
		height: 1rem;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
	.left{
		color: #666666;
		font-size: 0.35rem;
	}
	.right button{
		width: 50px;
		height: 50px;
		border: none;
		outline: none;
		margin: 0 0.15rem;	
		border-radius: 50%;	
	}
	.update{
		background: url(../../../static/image/icon_user.png) no-repeat;
		background-position: -7px -186px;
	}
	.delete{
		background: url(../../../static/image/icon_user.png) no-repeat;
		background-position: -78px -186px;
	}
</style>
